

#include "CxImgProcThread.h"
#include "CxImgProc_Imp_01.h"

using namespace ImgProcPart;

CxImgProcThread::CxImgProcThread(IplImage * pImage,_ShowFunc showfunc,_ShowCountFunc countfunc):
			m_spin(true),
			m_buffer(32)
{
	m_proc=new CxImgProc_Imp_01(pImage);
	sig_showimage.connect(0,boost::bind(showfunc,_1));
	sig_showcount.connect(0,boost::bind(countfunc,_1,_2));
}


CxImgProcThread::~CxImgProcThread()
{
	m_spin=false;
	m_thread.interrupt();
	m_thread.join();
	delete m_proc;
}


void	CxImgProcThread::RunFunc()
{
    cout<<"Proc Thread"<<boost::this_thread::get_id()<<endl;

	try
	{
		while(m_spin)
		{
            boost::this_thread::sleep(boost::posix_time::milliseconds(30));
			Mat _tmpmat=m_buffer.Pop();
			sig_showimage(_tmpmat);
			IplImage img=_tmpmat;

			int incar(0),_incar(0),outcar(0),_outcar(0);

            m_proc->HandleImage(&img,&_incar,&_outcar);

			if(((_incar-incar)!=0)|| ((_outcar-outcar)!=0))
				sig_showcount(_incar,_outcar);
			boost::this_thread::interruption_point();
		}
	}
	catch(boost::thread_interrupted &)
	{
	}
}
